package com.xx.sg.controller.req;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class FireJudge {

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime beginTime;

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private LocalDateTime endTime;

    private String eventType;

    private String name;

    private Integer pageNo = 1;

    private Integer pageSize = 10;

    private String regionIds;

    private Integer action;

    private Integer flag3;

    public String toJsonStr() {
        JSONObject json = new JSONObject();
        // 格式化为 ISO 8601 格式，带毫秒，末尾Z
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'")
                .withZone(ZoneOffset.UTC);
        if (this.beginTime != null) {
            String start = this.beginTime.atZone(ZoneId.of("Asia/Shanghai"))
                    .withZoneSameInstant(ZoneOffset.UTC)
                    .format(formatter);
            json.put("beginTime", start);

            if (this.endTime == null) {
                this.endTime = this.beginTime.plusDays(1L);
            }

            String end = this.endTime.atZone(ZoneId.of("Asia/Shanghai"))
                    .withZoneSameInstant(ZoneOffset.UTC)
                    .format(formatter);
            json.put("endTime", end);
        }

        if (StringUtils.isNotBlank(this.eventType)) {
            json.put("eventType", this.eventType);
        }
        if (StringUtils.isNotBlank(this.name)) {
            json.put("name", this.name);
        }
        if (StringUtils.isNotBlank(this.regionIds)) {
            json.put("regionIds", this.regionIds);
        }
        if (this.action != null) {
            json.put("action", this.action);
        }
        if (this.flag3 != null) {
            json.put("flag3", this.flag3);
        }
        json.put("pageNo", this.pageNo);
        json.put("pageSize", this.pageSize);

        return json.toJSONString();
    }

}
